using System;
using UnityEngine;

public class UplineUI : MonoBehaviour
{
	private GameObject uiobj;

	public UIEventListener moneybtn;

	public UIEventListener diamondbtn;

	public UIEventListener energybtn;

	public UIEventListener backbtn;

	public UIEventListener homebtn;

	public UIEventListener ADsBtn;

	public UILabel moneylabel;

	public UILabel diamondlabel;

	public UILabel energylabel;

	private long energystarttime;

	private bool calenergyflag;

	private int onetime = 300;

	private long curtime;

	private int intersec;

	public int caltime;

	private float temptime;

	public TweenPosition uplineanima;

	public TweenPosition parentanima;

	public UILabel AdLabel;

	public UISprite AdSp;

	public UISprite AdMoneySp;

	public TweenScale AdAnima;

	private float curmoney;

	private float curdiamond;

	private float curenergy;

	private float curmaxenergy;

	private float endmoney;

	private float enddiamond;

	private float endenergy;

	private float endmaxenergy;

	private bool numaddflag;

	private float stepmoney;

	private float stepdiamond;

	private float stepenergy;

	private float stepmaxenergy;

	private float numtime;

	private bool islostfocus;

	private void Start()
	{
		moneybtn.onClick = OnClickmoneyBtn;
		diamondbtn.onClick = OnClickdiamondBtn;
		energybtn.onClick = OnClickenergyBtn;
		backbtn.onClick = OnClickbackBtn;
		homebtn.onClick = OnClickhomeBtn;
		ADsBtn.onClick = OnClickAdsBtn;
	}

	private void Update()
	{
		if (calenergyflag)
		{
			temptime += Time.deltaTime;
			if (temptime >= 1f)
			{
				caltime--;
				if (caltime <= 0)
				{
					caltime = onetime;
					if (MenuPlayerprefs.instance.recoverEnergy(1))
					{
						calenergyflag = false;
					}
					else
					{
						calenergyflag = true;
						MenuPlayerprefs.instance.SetEnergyStarttime(DateTime.Now.Ticks.ToString());
					}
				}
				temptime = 0f;
			}
		}
		if (numaddflag)
		{
			numtime += Time.deltaTime;
			if (curmoney != endmoney)
			{
				curmoney += stepmoney * Time.deltaTime;
			}
			if (curdiamond != enddiamond)
			{
				curdiamond += stepdiamond * Time.deltaTime;
			}
			if (curenergy != endenergy)
			{
				curenergy += stepenergy * Time.deltaTime;
			}
			if (curmaxenergy != endmaxenergy)
			{
				curmaxenergy += stepmaxenergy * Time.deltaTime;
			}
			if (numtime > 0.5f)
			{
				curmoney = endmoney;
				curdiamond = enddiamond;
				curenergy = endenergy;
				curmaxenergy = endmaxenergy;
				numtime = 0f;
				numaddflag = false;
			}
			show_info();
		}
	}

	public void init_ui_info()
	{
		MenuPlayerprefs.instance.readBaseData();
		MenuPlayerprefs.instance.readpiece();
		infoUpdate();
		if (MenuPlayerprefs.instance.allenergy < MenuPlayerprefs.instance.maxenergy)
		{
			calenergyflag = true;
			energystarttime = long.Parse(MenuPlayerprefs.instance.EnergyStartTime);
			curtime = DateTime.Now.Ticks;
			intersec = (int)((curtime - energystarttime) / 10000000);
			if (intersec < 0)
			{
				intersec = 0;
			}
			if (intersec >= onetime)
			{
				if (MenuPlayerprefs.instance.recoverEnergy(intersec / onetime))
				{
					calenergyflag = false;
				}
				else
				{
					calenergyflag = true;
					caltime = onetime - intersec % onetime;
					energystarttime = (DateTime.Now.Ticks / 10000000 - intersec % onetime) * 10000000;
					MenuPlayerprefs.instance.SetEnergyStarttime(energystarttime.ToString());
				}
			}
			else
			{
				calenergyflag = true;
				caltime = onetime - intersec % onetime;
				energystarttime = (DateTime.Now.Ticks / 10000000 - intersec % onetime) * 10000000;
				MenuPlayerprefs.instance.SetEnergyStarttime(energystarttime.ToString());
			}
		}
		else
		{
			calenergyflag = false;
		}
		ShowAdsBtn();
	}

	public void infoUpdate(bool istrue = false)
	{
		if (GlobalInf.CurrentQuality < Quality.Low)
		{
			istrue = false;
		}
		if (!istrue)
		{
			curmoney = MenuPlayerprefs.instance.allmoney;
			curdiamond = MenuPlayerprefs.instance.alldiamond;
			curenergy = MenuPlayerprefs.instance.allenergy;
			curmaxenergy = MenuPlayerprefs.instance.maxenergy;
			show_info();
			numaddflag = false;
		}
		if (MenuPlayerprefs.instance.GetEnergyrota() < 1f)
		{
			calenergyflag = true;
			caltime = onetime;
		}
	}

	public void refershinfo()
	{
		numtime = 0f;
		endmoney = MenuPlayerprefs.instance.allmoney;
		enddiamond = MenuPlayerprefs.instance.alldiamond;
		endenergy = MenuPlayerprefs.instance.allenergy;
		endmaxenergy = MenuPlayerprefs.instance.maxenergy;
		stepmoney = (endmoney - curmoney) * 2f;
		stepdiamond = (enddiamond - curdiamond) * 2f;
		stepenergy = (endenergy - curenergy) * 2f;
		stepmaxenergy = (endmaxenergy - curmaxenergy) * 2f;
		if (GlobalInf.CurrentQuality > Quality.Low)
		{
			numaddflag = true;
			return;
		}
		numaddflag = false;
		curmoney = endmoney;
		curdiamond = enddiamond;
		curenergy = endenergy;
		curmaxenergy = endmaxenergy;
		numtime = 0f;
		show_info();
	}

	public void show_info()
	{
		moneylabel.text = $"{(int)curmoney:N0}";
		diamondlabel.text = $"{(int)curdiamond:N0}";
		energylabel.text = (int)curenergy + "/" + (int)curmaxenergy;
	}

	public void OnClickmoneyBtn(GameObject btn)
	{
		MonoInstance<AudioController>.instance.play(AudioType.UI_BTN1, 0f);
		MenuUIController.instance.enable_chargeui(0);
	}

	public void OnClickdiamondBtn(GameObject btn)
	{
		MonoInstance<AudioController>.instance.play(AudioType.UI_BTN1, 0f);
		MenuUIController.instance.enable_chargeui(1);
	}

	public void OnClickenergyBtn(GameObject btn)
	{
		MonoInstance<AudioController>.instance.play(AudioType.UI_BTN1, 0f);
		MenuUIController.instance.enable_chargeui(2);
	}

	public void OnClickbackBtn(GameObject btn)
	{
		MonoInstance<AudioController>.instance.play(AudioType.UI_BTN, 0f);
		MenuBackController.instance.BackFun();
	}

	public void OnClickhomeBtn(GameObject btn)
	{
		MenuBackController.instance.StroeGoFun();
	}

	public void OnClickAdsBtn(GameObject btn)
	{
		if (Platform.IsUnityAdsReady())
		{
			Platform.ShowRewardVideoUnityAds();
		}
		ShowAdsBtn();
	}

	public void ShowAdsBtn()
	{
		if (!Platform.IsUnityAdsCanShow())
		{
			NGUITools.SetActive(AdSp.gameObject, state: false);
			return;
		}
		NGUITools.SetActive(AdSp.gameObject, state: true);
		if (Platform.IsUnityAdsReady())
		{
			AdSp.spriteName = "GY_shiPinGuangGao";
			AdAnima.enabled = true;
			AdAnima.PlayForward();
		}
		else
		{
			AdSp.spriteName = "GY_shiPinGuangGaoHui";
			AdAnima.ResetToBeginning();
			AdAnima.enabled = false;
		}
		if (MenuPlayerprefs.IsDiamondADReward)
		{
			AdMoneySp.spriteName = "GY_zuanShi_01";
			AdLabel.text = "+1";
		}
		else
		{
			AdMoneySp.spriteName = "CZ_jinBi_02";
			AdLabel.text = "+100";
		}
	}

	public void AnimaPlayF()
	{
		uplineanima.PlayForward();
	}

	public void AnimaPlayR()
	{
		uplineanima.PlayReverse();
	}

	public void parentanimaPF()
	{
		parentanima.PlayForward();
	}

	public void setfixedpos()
	{
		parentanima.transform.localPosition = Vector3.zero;
	}

	public void enable()
	{
		if (uiobj == null)
		{
			uiobj = base.gameObject;
		}
		NGUITools.SetActive(uiobj, state: true);
		init_ui_info();
	}

	public void disable()
	{
		NGUITools.SetActive(uiobj, state: false);
	}

	private void OnApplicationFocus(bool isfocus)
	{
		if (isfocus)
		{
			if (islostfocus)
			{
				init_ui_info();
			}
		}
		else
		{
			islostfocus = true;
		}
	}
}
